package com.clps;

import com.clps.sync.NotifyService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class springbootTest001 {

    @Autowired
    private NotifyService notifyService;

    @Test
    public void  execute(){
        log.info("your method test Code");
        log.info("Invoking an asynchronous method. " + Thread.currentThread().getName());
        notifyService.noAsync();
        notifyService.withAsync();
    }

    @Test
    @Async("threadPoolTaskExecutor")
    public  void  execute01(){
        int ss = 12/0;
    }

    @Test
    public void testAsyncAnnotationForMethodsWithReturnType()
            throws InterruptedException, ExecutionException {
        log.info("Invoking an asynchronous method. " + Thread.currentThread().getName());
        Future<String> future = notifyService.asyncMethodWithReturnType();

        while (true) {
            if (future.isDone()) {
                log.info("Result from asynchronous process - " + future.get());
                break;
            }
            log.info("Continue doing something else. ");
            Thread.sleep(1000);
        }
    }


    @Test
    public void testAsyncAnnotationForMergedServicesResponse() throws InterruptedException, ExecutionException {
        log.info("Invoking an asynchronous method. " + Thread.currentThread().getName());
        CompletableFuture<String> completableFuture = notifyService.asyncMergeServicesResponse();

        while (true) {
            if (completableFuture.isDone()) {
                log.info("Result from asynchronous process - " + completableFuture.get());
                break;
            }
            log.info("Continue doing something else. ");
            Thread.sleep(1000);
        }
    }
}
